<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      v-show="showSearch"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="90px"
    >
      <!-- 时间选择 -->
      <el-form-item :label="$t('时间选择')">
        <el-date-picker
          style="width: 350px"
          v-model="queryParams.time"
          type="daterange"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <!-- 租户 -->
      <el-form-item label="租户">
        <el-input v-model="queryParams.distrName" />
      </el-form-item>
      <!-- 支付方式 -->
      <el-form-item label="支付方式">
        <el-input v-model="queryParams.distrName" />
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handleGen"
          :disabled="tableCheck.length == 0"
          v-hasPermi="['distribution:user:insertUserByPlatform']"
        >
          生成发票
        </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>
    <el-table
      v-loading="loading"
        ref="TableRef"  :data="tableData"
      @selection-change="(rows) => (tableCheck = rows)"
    >
      <el-table-column
        show-overflow-tooltip
        type="selection"
        width="50"
        align="center"
      />
      <!-- 租户 支付方式 账单数量 金额 支付时间 操作 -->
      <!-- 租户 -->
      <el-table-column
        show-overflow-tooltip
        label="租户"
        align="center"
        prop="distrType"
      >
        <template slot-scope="{ row }">
          <!-- 租户类型 sys_user:平台 seller:卖家 distributor:商家 -->
          <span style="color: #409eff" v-if="row.distrType == 'sys_user'">
            平台
          </span>
          <span style="color: #67c23a" v-if="row.distrType == 'seller'">
            卖家
          </span>
          <span style="color: #e6a23c" v-if="row.distrType == 'distributor'">
            商家
          </span>
        </template>
      </el-table-column>
      <!-- 支付方式 -->
      <el-table-column
        show-overflow-tooltip
        label="支付方式"
        align="center"
        prop="distrId"
      />
      <!-- 账单数量 -->
      <el-table-column
        show-overflow-tooltip
        label="账单数量"
        align="center"
        prop="distrName"
      />
      <!-- 金额 -->
      <el-table-column
        show-overflow-tooltip
        label="金额"
        align="center"
        prop="userName"
      />
      <!-- 支付时间 -->
      <el-table-column
        show-overflow-tooltip
        label="支付时间"
        align="center"
        prop="roleName"
      />
      <el-table-column
        show-overflow-tooltip
        :label="$t('操作')"
        align="center"
        width="160"
      >
        <template slot-scope="scope">
          <el-button type="text" @click="handleDetail">
            <!-- 查看详情 -->
            {{ $t("详情") }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 详情 -->
    <Detail
      :show.sync="detail.show"
      @getList="getList"
      :formData="detail.formData"
      :type="detail.type"
    />
  </div>
</template>

<script>
import { getUserList, updateUserByPlatform } from "@/api/platform/userManage";
import { checkOps } from "@/api/platform/supplierManage";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import Detail from "./detail.vue";
export default {
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  name: "User",
  components: { Treeselect, Detail },
  data() {
    return {
      /**新增/修改 */
      detail: {
        show: false,
        type: "",
        formData: {},
      },
      // 查询参数
      queryParams: {
        time: [],
        pageNum: 1,
        pageSize: 10,
      },

      tableData: [],
      tableCheck: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 查询参数
    };
  },
  watch: {},
  created() {
    this.queryParams.time = this.setStatisticsDefautTime("day");
    this.getList();
  },
  methods: {
    handleDetail(row) {
      this.detail.formData = row;
      this.detail.show = true;
    },
    handleGen() {},
    // 帐号状态 status  0:正常 1:停用
    handleStatusChange(row) {
      const data = {
        ...row,
        targetRoleId: row.roleId,
      };
      console.log(`data ==>`, JSON.parse(JSON.stringify(data)));
      updateUserByPlatform(data)
        .then((res) => {
          const { code, data, msg } = res;
          if (code === 200) {
            this.$message({ type: 'success', message: msg})
;
            this.getList();
          }
        })
        .catch(() => {
          row.status == "0" ? (row.status = "1") : (row.status = "0");
        });
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      getUserList(this.queryParams).then((response) => {
        this.tableData = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleUpdate" /**修改信息 */:
          this.handleUpdate(row);
          break;
        case "handleUpdatePwd" /**修改密码 */:
          this.editPassword.formData = row;
          this.editPassword.show = true;
          break;
        default:
          break;
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      checkOps(1).then((res) => {
        const { code } = res;
        if (code === 200) {
          this.addAndEdit.type = "add";
          this.addAndEdit.show = true;
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.addAndEdit.type = "edit";
      this.addAndEdit.formData = row;
      this.addAndEdit.show = true;
    },
  },
};
</script>
